package com.hongkzh.www.user.controller;

import com.hongkzh.www.conmon.ReqResponse;
import com.hongkzh.www.swagger.annotation.*;
import com.hongkzh.www.user.service.UserService;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;

import java.util.List;

/**
 * @author martins
 */
@Api(tag = "user", description = "用户接口")
public class UserController extends Controller {
    static UserService userService = new UserService();
    @ApiOperation(url = "/v1/user/findDayIntegral", tag = "user", httpMethod = "post",description = "通过用户id 返回用户当日收益")
    @Params({
            @Param(name = "loginUid", description = "当前用户id", required = true, dataType = "string")
    })
    @ApiResponses({
                   @ApiResponse(code=200,message="成功",responseHeaders={
                           @ResponseHeader(name="code",description = " 0成功 1失败"),
                           @ResponseHeader(name="data",description = "integral 用户积分"),
                           @ResponseHeader(name="msg",description = "失败原因") })

            })
    public void findDayIntegral(){
        String id=getPara("loginUid");
        ReqResponse<Record> r=new ReqResponse();
        Record record= userService.findDayIntegral(id);
        r.setData(record);
        renderJson(r);
    }

    @ApiOperation(url = "/v1/user/followList", tag = "user", httpMethod = "post",description = "通过用户id 返回用户的关注列表")
    @Params({
            @Param(name = "loginUid", description = "当前用户id", required = true, dataType = "string"),
            @Param(name = "pageNumber", description = "页码", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = "totalRow 总行数  pageNumber第几页 firstPage 是否第一页 lastPage 是否最后一页 totalPage 总页数 pageSize每页条数  list[name 名称 ,uid 用户id headImg 头像 introduction 简介 followUserId 自媒体或者企业id,fcount 关注总数]"),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    public void followList(){
        String id=getPara("loginUid");
        int pageNumber=getParaToInt("pageNumber");
        ReqResponse< Page<Record>> r=new ReqResponse();
        Page<Record> record= userService.followList(id,pageNumber);
        r.setData(record);
        renderJson(r);
    }
    @ApiOperation(url = "/v1/user/followDelete", tag = "user", httpMethod = "post",description = "通过用户id 关注id 取消关注")
    @Params({
            @Param(name = "loginUid", description = "当前用户id", required = true, dataType = "string"),
            @Param(name = "followUserId", description = "被关注id", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    @Before(Tx.class)
    public void followDelete(){
        String followUserId=getPara("followUserId");
        String uid=getPara("loginUid");
        ReqResponse<String> r=new ReqResponse();
          try {
              userService.followDelete(uid,followUserId);
          }catch (Exception e){
              e.printStackTrace();
             r.setCode(1);
             r.setMsg("取消关注失败");
          }
        renderJson(r);
    }
    @ApiOperation(url = "/v1/user/historyList", tag = "user", httpMethod = "post",description = "通过用户id 返回用户的历史记录")
    @Params({
            @Param(name = "loginUid", description = "当前用户id", required = true, dataType = "string"),
            @Param(name = "pageNumber", description = "页码", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = "totalRow 总行数  pageNumber第几页 firstPage 是否第一页 lastPage 是否最后一页 totalPage 总页数 pageSize每页条数  list[name 名称 ,id 记录id headImg 头像 title 标题 uid 自媒体或者企业id coverImgSrc 图片 type 类型  1自媒体 2企业广告3城市广告4传统文化 ]"),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    public void historyList(){
        String id=getPara("loginUid");
        int pageNumber=getParaToInt("pageNumber");
        ReqResponse< Page<Record>> r=new ReqResponse();
        Page<Record> record= userService.historyList(id,pageNumber);
        r.setData(record);
        renderJson(r);
    }
    @ApiOperation(url = "/v1/user/historyDelete", tag = "user", httpMethod = "post",description = "通过id 删除历史记录")
    @Params({
            @Param(name = "id", description = "id", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    @Before(Tx.class)
    public void historyDelete(){
        String id=getPara("id");

        ReqResponse<String> r=new ReqResponse();
        try {
            userService.historyDelete(id);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(1);
            r.setMsg("删除历史观看记录失败");
        }
        renderJson(r);
    }
    @ApiOperation(url = "/v1/user/historyAdd", tag = "user", httpMethod = "post",description = "通过用户id   视频id 增加播放历史 返回奖励信息")
    @Params({
            @Param(name = "uid", description = "用户id", required = true, dataType = "string"),
            @Param(name = "advId", description = "视频id", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    @Before(Tx.class)
    public void historyAdd(){
        String uid=getPara("uid");
        String advId=getPara("advId");
        ReqResponse<List<Record>> r=new ReqResponse();
        try {
            List<Record> rs= userService.historyAdd(advId,uid);
            r.setData(rs);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(1);
            r.setMsg("关注失败");
        }
        renderJson(r);
    }


    @ApiOperation(url = "/v1/user/followAdd", tag = "user", httpMethod = "post",description = "通过用户id   关注")
    @Params({
            @Param(name = "loginUid", description = "当前用户id", required = true, dataType = "string"),
            @Param(name = "followUserId", description = "被关注id", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败 data  记录id"),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    @Before(Tx.class)
    public void followAdd(){
        String id=getPara("loginUid");
        String followUserId=getPara("followUserId");
        ReqResponse<String> r=new ReqResponse();
        try {
            Record record= userService.followAdd(followUserId,id);
            r.setData(record.getStr("id"));
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(1);
            r.setMsg("关注失败");
        }
        renderJson(r);
    }


    @ApiOperation(url = "/v1/user/getUserInfo", tag = "user", httpMethod = "post",description = "通过用户id 获取企业信息")
    @Params({
            @Param(name = "loginUid", description = "当前登录用户id", required = true, dataType = "string"),
            @Param(name = "uid", description = "用户id", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = "{ id 用户id,headImg头像 ,name 用户名称 background 背景图 describe 简介 address 地址 companyName 公司名称 tel 联系电话 introduce 介绍 allCount 视频总数 followCount 关注总数 playCount 播放总数  followId 关注id  followState 已经关注 1     没关注 0}"),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    @Before(Tx.class)
    public void getUserInfo(){
        String uid=getPara("uid");
        String loginUid=getPara("loginUid");
        ReqResponse<Record> r=new ReqResponse();
        try {
            Record record= userService.getUserInfo(loginUid,uid);
            r.setData(record);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(1);

        }
        renderJson(r);
    }

    @ApiOperation(url = "/v1/user/getUserAdvList", tag = "user", httpMethod = "post",description = "通过用户id 获取视频列表")
    @Params({
            @Param(name = "uid", description = "用户id", required = true, dataType = "string"),
            @Param(name = "pageNumber", description = "页码", required = true, dataType = "string"),
            @Param(name = "sortId", description = "排序字段 默认排序 rank  最多浏览 play_count  最新上传 create_date ", required = false, dataType = "string"),
            @Param(name = "sortValue", description = "排序值 asc 升 desc 降 ", required = false, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = "totalRow 总行数  pageNumber第几页 firstPage 是否第一页 lastPage 是否最后一页 totalPage 总页数 pageSize每页条数  list[coverImgSrc 封面图片地址 imgSrc 视频文件地址 title 标题 note 说明 id 视频id  type 类型  1自媒体 2企业广告3城市广告4传统文化 ]"),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })

    public void getUserAdvList(){
        String uid=getPara("uid");
        int pageNumber=getParaToInt("pageNumber");
        String sortId=getPara("sortId");
        String sortValue=getPara("sortValue");
        if(StrKit.isBlank(sortId)){
            sortId="rank";
        }
        if(StrKit.isBlank(sortValue)){
            sortValue="desc";
        }
        ReqResponse<Page<Record>> r=new ReqResponse();
        try {
            Page<Record> record= userService.getUserAdvList(uid,pageNumber,sortId,sortValue);
            r.setData(record);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(1);

        }
        renderJson(r);
    }
    @ApiOperation(url = "/v1/user/getAllAlbumByUserId", tag = "user", httpMethod = "post",description = "通过用户id 获取相册列表")
    @Params({
            @Param(name = "uid", description = "用户id", required = true, dataType = "string"),
            @Param(name = "pageNumber", description = "页码", required = true, dataType = "string"),
            @Param(name = "sortId", description = "排序字段 默认排序 rank  最多浏览 view_count  最新上传 create_date ", required = false, dataType = "string"),
            @Param(name = "sortValue", description = "排序值 asc 升 desc 降 ", required = false, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = "totalRow 总行数  pageNumber第几页 firstPage 是否第一页 lastPage 是否最后一页 totalPage 总页数 pageSize每页条数  list[ coverPhoto 文件地址 name 标题 rank 序号 viewCount 查看总数 id  details[ imgSrc 文件地址  name 名称  rank 序号 id] ]"),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })

    public void getAllAlbumByUserId(){
        String uid=getPara("uid");
        int pageNumber=getParaToInt("pageNumber");
        String sortId=getPara("sortId");
        String sortValue=getPara("sortValue");
        if(StrKit.isBlank(sortId)){
            sortId="rank";
        }
        if(StrKit.isBlank(sortValue)){
            sortValue="desc";
        }
        ReqResponse<Page<Record>> r=new ReqResponse();
        try {
            Page<Record> record= userService.getAllAlbumByUserId(uid,pageNumber,sortId,sortValue);
            r.setData(record);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(1);

        }
        renderJson(r);
    }
    @ApiOperation(url = "/v1/user/getAllProductByUserId", tag = "user", httpMethod = "post",description = "通过用户id 获取产品列表")
    @Params({
            @Param(name = "uid", description = "用户id", required = true, dataType = "string"),
            @Param(name = "pageNumber", description = "页码", required = true, dataType = "string"),
            @Param(name = "sortId", description = "排序字段 默认排序 rank  最多浏览 view_count  最新上传 create_date ", required = false, dataType = "string"),
            @Param(name = "sortValue", description = "排序值 asc 升 desc 降 ", required = false, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = "totalRow 总行数  pageNumber第几页 firstPage 是否第一页 lastPage 是否最后一页 totalPage 总页数 pageSize每页条数  list[ imgSrc 地址 title 标题 subtitle 副标题 rank 序号 preorderNum 预约数 orderNum 购买数   id state 状态 1预约 2 购买  price 价格  ]"),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })

    public void getAllProductByUserId(){
        String uid=getPara("uid");
        int pageNumber=getParaToInt("pageNumber");
        String sortId=getPara("sortId");
        String sortValue=getPara("sortValue");
        if(StrKit.isBlank(sortId)){
            sortId="rank";
        }
        if(StrKit.isBlank(sortValue)){
            sortValue="desc";
        }
        ReqResponse<Page<Record>> r=new ReqResponse();
        try {
            Page<Record> record= userService.getAllProductByUserId(uid,pageNumber,sortId,sortValue);
            r.setData(record);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(1);

        }
        renderJson(r);
    }


    @ApiOperation(url = "/v1/user/login", tag = "user", httpMethod = "post",description = "通过用户手机号 密码登录")
    @Params({
            @Param(name = "mobile", description = "用户 手机", required = true, dataType = "string"),
            @Param(name = "password", description = "密码", required = true, dataType = "string")

    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = " id 用户id headImg 头像 name 名称 token 融云token  userToken 系统token"),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })

    public void login(){
        String mobile=getPara("mobile");
        String password=getPara("password");
        ReqResponse< Record> r=new ReqResponse();
        Record record=userService.login(mobile,password);
        r.setData(record);
        renderJson(r);
    }

}
